using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

using AmazonBookLinker.Diagnostics;

using WindowsLive.Writer.Api;

namespace AmazonBookLinker
{
  public partial class HtmlCodeViewerForm : Form
  {
    public HtmlCodeViewerForm()
    {
      InitializeComponent();
    }


    public string HtmlText
    {
      get { return HtmlServices.HtmlDecode( txtHtmlCode.Text ); }
      set { txtHtmlCode.Text = HtmlServices.HtmlDecode( HtmlServices.HtmlEncode( value ) ); }
    }

    public bool Wrap
    {
      get { return txtHtmlCode.WordWrap; }
      set { txtHtmlCode.WordWrap = value; }
    }

    public bool ReadOnly
    {
      get { return txtHtmlCode.ReadOnly; }
      set { txtHtmlCode.ReadOnly = value; }
    }

    public Button DefaultButton
    {
      get { return btnDefault; }
    }

    public bool EnableMacrosQuickInsertContextMenu
    {
      set { 
        MacrosContextMenu.Enabled = value;
        lblTip.Visible = value;
      }
    }


    private void btnDefault_Click( object sender, EventArgs e )
    {
      string mode = Tag as string;

      if ( mode == "Modify" )
      {
        try
        {
          File.WriteAllText( Context.TemplateFileName ?? PluginHelper.HtmlTemplateFilename, HtmlServices.HtmlDecode( txtHtmlCode.Text ) );
        }
        catch ( Exception ex )
        {
          PluginTracer.LogException( ex, "Modifying the template file", new Dictionary<string, string>
                                                                        {
                                                                            {
                                                                                "HtmlTemplateFileName", PluginHelper.HtmlTemplateFilename
                                                                                }
                                                                        } );
          PluginTracer.DisplayUnexpectedException( ex );
        }
      }
      DialogResult = DialogResult.OK;
    }


    private void btnCancel_Click( object sender, EventArgs e )
    {
      DialogResult = DialogResult.Cancel;
    }

    private void btnMacros_Click( object sender, EventArgs e )
    {
      using ( TemplateTokensFormView form = new TemplateTokensFormView() )
      {
        form.ShowDialog();
      }
    }

    private void btnSaveAs_Click(object sender, EventArgs e)
    {
      DialogResult dialogResult = this.saveTemplateAsDialog.ShowDialog();

      if (dialogResult == DialogResult.OK)
      {
        string fileName = this.saveTemplateAsDialog.FileName;
        Context.TemplateFileName = fileName;
        File.WriteAllText( fileName, HtmlServices.HtmlDecode(txtHtmlCode.Text) );
        Context.TemplateContent = File.ReadAllText(fileName);
        DialogResult = DialogResult.OK;
      }
    }

    private void HtmlCodeViewerForm_Load(object sender, EventArgs e)
    {
      // Show the "Save As..." button if in modified mode.
      btnSaveAs.Visible = (Tag.ToString() == "Modify");
    }


    private void MacrosContextMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
      ContextMenuStrip contextMenuStrip = (ContextMenuStrip) sender;
      TextBox parent = (TextBox) contextMenuStrip.SourceControl;
      string insertText = string.Empty;
      switch (e.ClickedItem.Text.ToLower())
      {
        case "asin":
          insertText = Tokens.BookAsin;
          break;
        case "author":
          insertText = Tokens.BookAuthor;
          break;
        case "description":
          insertText = Tokens.BookDescription;
          break;
        case "image link":
          insertText = Tokens.BookImageUrl;
          break;
        case "isbn 10":
          insertText = Tokens.BookIsbn10;
          break;
        case "isbn 13":
          insertText = Tokens.BookIsbn13;
          break;
        case "link":
          insertText = Tokens.BookDetailsUrl;
          break;
        case "number of pages":
          insertText = Tokens.BookNumberOfPages;
          break;
        case "price":
          insertText = Tokens.BookPrice;
          break;
        case "publication date":
          insertText = Tokens.BookPublicationDate;
          break;
        case "publisher":
          insertText = Tokens.BookPublisher;
          break;
        case "rating":
          insertText = Tokens.BookRating;
          break;
        case "title":
          insertText = Tokens.BookTitle;
          break;
      }

      int previousStartLocation = parent.SelectionStart;
      parent.Text = parent.Text.Insert( parent.SelectionStart, insertText );
      parent.SelectionStart = previousStartLocation + insertText.Length;
    }
  }
}